package com.pdsu.alumni.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pdsu.alumni.common.annotation.NoAuthentication;
import com.pdsu.alumni.common.result.Result;
import com.pdsu.alumni.pojo.CommonUser;
import com.pdsu.alumni.service.ICommonUserService;
import com.pdsu.alumni.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @program: alumni
 * @description: 优秀校友控制层
 * @author: Acerola
 * @create: 2022-01-31 15:55
 **/


@Api(tags = "优秀校友控制层")
@Slf4j
@RestController
@RequestMapping("/excellentuser")
public class ExcellentUserController {

    @Autowired
    private ICommonUserService commonUserService;


    @ApiOperation(value = "获取所有优秀校友")
    @GetMapping("/list")
    @NoAuthentication
//    @RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result getAllUser() {
        QueryWrapper<CommonUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", 0);
        queryWrapper.eq("cuser_niceuser",1);
        List<CommonUser> list = commonUserService.list(queryWrapper);
        return new Result().success().put(list);
    }

    @ApiOperation(value = "通过id获取优秀校友")
    @PostMapping("/getOne")
    @NoAuthentication
//  @RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result getUserById(@RequestBody Map map) {
        Object object = map.get("id");
        Integer id = JSON.parseObject(JSON.toJSONString(object), Integer.class);
        QueryWrapper<CommonUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cuser_id", id);
        queryWrapper.eq("cuser_niceuser",1);
        queryWrapper.eq("state", 0);
        CommonUser commonUser = commonUserService.getOne(queryWrapper);
        if (commonUser == null) {
            return new Result().success("你查询的用户不存在");
        }
        return new Result().success().put(commonUser);
    }

    @ApiOperation(value = "更新优秀校友信息")
    @PostMapping("/update")
    @RequiresAuthentication
//    @RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result<?> userUpdate(@RequestBody Map map) {
        Object res = map.get("user");
        CommonUser commonUser = JSON.parseObject(JSON.toJSONString(res), CommonUser.class);
        commonUserService.updateById(commonUser);
        return new Result<>().success();
    }

    @ApiOperation(value = "删除优秀校友")
    @PostMapping("/delete")
    @RequiresAuthentication
//    @RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result<?> userDelete(@RequestBody Map map) {
        Object res = map.get("user");
        CommonUser commonUser = JSON.parseObject(JSON.toJSONString(res), CommonUser.class);
        commonUser.setCuserNiceuser(0);
        commonUserService.updateById(commonUser);
        return new Result<>().success();
    }

    @ApiOperation(value = "添加优秀校友")
    @PostMapping("/add")
    @RequiresAuthentication
//    @RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result<?> userInsterByHand(@RequestBody Map map) {
        Object res = map.get("user");
        CommonUser commonUser = JSON.parseObject(JSON.toJSONString(res), CommonUser.class);
        commonUser.setCuserNiceuser(1);
        commonUserService.updateById(commonUser);
        return new Result<>().success();
    }
}
